#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <stdint.h>
#include "include/MQI_c_interface.h"
#include "include/readData.hpp"
using namespace std;

// 判断是否为Windows平台
#if defined(_WIN32) || defined(_WIN64)
const char PATH_SEPARATOR = '\\';
const std::string PATH_SEPARATOR_STR = "\\";
#else
const char PATH_SEPARATOR = '/';
    const std::string PATH_SEPARATOR_STR = "/";
#endif

int main(int argc,char *argv[]){
  cout << "test2 mqi with 0 offset" << endl;
  if(argc != 4){
    cout<<"参数错误!";
    exit(-1);
  }

  string file = argv[1];
  int mqi_no = atoi(argv[2]);
  int random_seed = atoi(argv[3]);

  cout<<"file:"<<file<<" mqi_no:"<<mqi_no<<" seed:"<<random_seed<<endl;

  srand(random_seed);
  string graph_file = ".." + PATH_SEPARATOR_STR + "instance" + PATH_SEPARATOR_STR + file + ".graph";
  string seed_input = ".." + PATH_SEPARATOR_STR + "MQI_BKV" + PATH_SEPARATOR_STR + file + PATH_SEPARATOR_STR + file + "_result_" + to_string(mqi_no) + ".smat";

//    string graph_file = "..\\instance\\" + file + ".graph";
//  string seed_input = "..\\MQI_BKV\\" + file + "\\" + file +"_result_"+to_string(mqi_no) + ".smat";

  cout<<"graph file:"<<graph_file<<endl;
  cout<<"seed file"<<seed_input<<endl;
  string result_output = seed_input;
  //读图
  int64_t m = 0, n = 0;
  int64_t* ai = NULL, * aj = NULL;
  double* a = NULL;
  read_and_convert<int64_t, int64_t>(graph_file.c_str(), &m, &n, &ai, &aj, &a);

  //Read seed
  int64_t nR = 0;
  int64_t* R = NULL;
  read_seed<int64_t, int64_t>(seed_input.c_str(), &nR, &R);
  int64_t* ret_set = new int64_t[nR];

  // free(a);
  //Begin calling C function
  cout << "calling C function" << endl;
  int64_t actual_length = MQI64(m, nR, ai, aj, 0, R, ret_set);
  cout << "output set" << endl;

  cout<< "total number of vertices is " << actual_length << endl;

  delete[]R;
  delete[]ai;
  delete[]aj;

  ofstream outpartition(result_output);
  if (!outpartition) {
    cout << "输出文件打开失败" << endl;
    exit(1);
  }
  //save
  outpartition << actual_length << endl;
  for (int i = 0; i < actual_length; i++) {
    outpartition << ret_set[i] << endl;
  }
  outpartition.close();
  delete [] ret_set;
}